// 这份代码专门处理逻辑层的操作
const questionDao = require('../dao/questionDao');
const powerDao = require('../dao/powerDao');

let imgMap = {
    '政治': '/images/zhengzhi.png',
    '英语': '/images/yingyu.png',
    '高数': '/images/shuxue.png'
}

const questionService = {
    /*
     * 获取公共课题库组
     */
    async getgonggongkeQuestionGroup(uid) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '查看题库');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            let queryResult = await questionDao.getgonggongkeQuestionGroup();
            // 过滤数据
            let questionGroup = [];
            for (let i = 0; i < queryResult.length; i++) {
                let item_name = queryResult[i].name;
                let img = imgMap[item_name];
                let question = {
                    id: i+1,
                    name: item_name,
                    count: queryResult[i]._count,
                    img
                };
                questionGroup.push(question);
            }

            result = { success: true, msg: '获取公共课题库组成功', data: { questionGroup } };
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    /*
     * 获取专业课题库组
     */
    async getzhuanyekeQuestionGroup(uid) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '查看题库');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            let queryResult = await questionDao.getzhuanyekeQuestionGroup();
            // 过滤数据
            let questionGroup = [];
            for (let i = 0; i < queryResult.length; i++) {
                let question = {
                    id: i+1,
                    name: queryResult[i].name,
                    count: queryResult[i]._count
                };
                questionGroup.push(question);
            }

            result = { success: true, msg: '获取专业课题库组成功', data: { questionGroup } };
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    async getQuestionByGonggongke(gonggongke, uid) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '查看题库');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            let data = await questionDao.getQuestionByGonggongke(gonggongke);
            if (data.length) {
                result = { success: true, msg: '加载题库内容成功', data };
            } else {
                result = { success: false, msg: '题库内容不存在' };
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },

    async getQuestionContent(qid, uid) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '查看题库');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            let data = await questionDao.getQuestionContent(qid);
            if (data.length) {
                result = { success: true, msg: '加载题库内容成功', data: data[0] };
            } else {
                result = { success: false, msg: '题库内容不存在' };
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    async getAllQuestion(uid, page) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '题库管理');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            let data = await questionDao.getAllQuestion(page);
            if (data.length) {
                result = { success: true, msg: '加载题库列表成功', data };
            } else {
                result = { success: false, msg: '题库内容不存在' };
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    }
};

module.exports = questionService;
